import Vue from 'vue'
import Vuex from 'vuex'
import {
    userInfoApi
} from '@/api/index'
Vue.use(Vuex)

export default new Vuex.Store({
    // sessionStorage.getItem('state') ? JSON.parse(sessionStorage.getItem('state')) :
    state: {
        userMsg: {},
        message: null,
        leftCollapse: false,
        braBrandModel: [{
            name: '华为',
            models: ['TE20', 'TE30', 'TE40', 'TE50', '9035A', '9039S'],
            value: 1
        }, {
            name: '中兴',
            models: ['T600', 'T700', 'T800'],
            value: 2
        }, {
            name: '宝利通',
            models: ['HDX7000', 'HDX8000', 'group550'],
            value: 3
        }, {
            name: '科达',
            models: ['H650', 'H850', 'H900', '7920'],
            value: 4
        }, {
            name: '思科',
            models: ['SX20'],
            value: 5
        }, {
            name: '其它',
            models: ['其他'],
            value: 6
        }],
    },

    mutations: {
        saveUserInfo(state, obj) {
            state.userMsg = obj
        },
        toggleLeftCollapse(state) {
            state.leftCollapse = !state.leftCollapse
        }
    },
    actions: {
        getUserInfo({
            commit,
            state
        }) {
            userInfoApi.get()
                .then(res => {
                    if (res.success === true) {
                        commit('saveUserInfo', res.data)
                        // console.log(res.data, '登录数据');
                        sessionStorage.setItem('info', JSON.stringify(res.data))
                        // console.log(JSON.parse(sessionStorage.getItem("info")), 'info');
                    } else {
                        state.message = this.$message.error('用户信息获取失败')
                    }
                })
                .catch(err => {
                    console.log(err);
                })
        }
    },
    getters: {
        // 品牌选择
        filterBrandName(state) {
            return (value) => {
                let brand = state.braBrandModel.find((ele) => {
                    return ele.value == value;
                })
                let name = brand ? brand.name : '其他'
                return name
            }
        }
    },
})